<?php 
namespace App\Http\Controllers\Admin;
// use App\Http\Controllers\org\upload;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use  App\Http\Model\Lists;
use Input;
class ListController extends CommonController {

	public function index(){
		$list = DB::table('list')->get();
		$ls = $list[0];
		$ls->content = post_html($ls->content);
		$data = DB::table('cate')->get(); //获取所有数据
		$arr = $this->digui($data,0);
		return view('admin.list',['data' => $arr,'ls'=>$ls]);
	}

	// 添加详细数据
	public function add(Request $request){
			if($request->isMethod('post')){
				$test = Input::all();
				Lists::ad($request,$this);
				$title		 = post_check($_POST['title']);
				$pid		 = intval($_POST['pid']);
				$keywords 	 = post_check($_POST['keywords']);
				$description = post_check($_POST['description']);
				$content 	 = $_POST['content'];
				// echo $content;die;
				// dump($content);die;
				// $content 	 = substr($content,3,-4);//把P便签去掉
				$id = DB::table('list')->insertGetId(
				 	[
					 	'title' 		=> $title,
					 	'pid' 			=> $pid,
					 	'keywords' 		=> $keywords,
					 	'description' 	=> $description,
					 	'content'		=> $content
				  	]
				);
				
				if($id){
					echo '添加成功';
				}else{
					echo '添加失败';
				}
				echo  '<a href="' . url('admin/l') .'">点击页面跳转</a>';
			}

	}

	public function ListAll(){
		$data = DB::table('cate')->get(); //获取所有数据
		$data = $this->digui($data,0);
		// $data = DB::table('cate')->where('pid',0)->get();
		return view('admin.listall',['data'=>$data]);
	}
	//文章的详情
	public function Listlist($id){
		$id = intval($id);
		// $data = DB::table('list')->where('pid',$id)->get();

	$data = DB::table('list')
			->leftJoin('cate', 'list.pid', '=', 'cate.cid')
			->where('list.pid',$id)
			->get();
		return view('admin.listlist',['data'=>$data]);
	}

	//编辑文章
	public function ListBianJi($id){
		$id = intval($id);
		$users = DB::select("select * from blog_list where id=7");
		$list = DB::table('list')->where('id',$id)->get(); //数据
		$ls = $list[0];
		// echo $ls->content;die;
		// $ls->content = post_html($ls->content);
		$data = DB::table('cate')->get(); //获取所有分类数据
		$st = array(); //
		$st['cid'] = $ls->pid;
		foreach ($data as $key => $value) {
			if($value->cid == $ls->pid){
				$st['name'] = $value->name;
			}
		}
		$arr = $this->digui($data,0);

		return view('admin.lsbj',['data' => $arr,'ls'=>$ls,'st'=>$st]);
	}

	//上传图片
	public function uploads(Request $request){
		if($request->hasFile('uploadFile')){
			$file = $request->file('uploadFile');
			$clientName = $file -> getClientOriginalName();
			echo "{$clientName}<br>";
		}
		return 'hello world';
	}
}